package view.Papers;

import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JList;
import javax.swing.JButton;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import javax.swing.JLabel;

import SQL.DB;
import view.MainFrame;
import view.userProfile.SubProgramChairView;

import java.awt.Color;
import java.awt.Font;
/**
 * This provides the view for the SubProgram Chair to submit Recommendations
 * 
 * @author Thang Tran, Emily Linniman
 * @version March 20, 2014
 */
public class SubmitRecommendationView extends JPanel {
	
	/** The last panel the frame was associated with. */
	JPanel lastPanel;
	
	/** The frame the panel is associated with. */
	MainFrame my_frame;
	
	/** A JList of the papers that the subprogram chair has. */
	private JList paperList;

	//TODO
	private String conferenceName;
	
	private JLabel paperfileNameLabel;

	private JLabel fileLabel;
	
	/**
	 * Constructs the view to submit a recommendation
	 * 
	 * @param the_frame the frame to associate the panel with
	 * @param the_panel the panel that was last associated with the frame
	 */
	@SuppressWarnings("unchecked")
	public SubmitRecommendationView(MainFrame the_frame, String the_conference) {
		setBackground(new Color(245, 255, 250));
		
		my_frame = the_frame;
		conferenceName = the_conference;
		
		
		setLayout(null);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(10, 36, 197, 184);
		add(scrollPane);
		
		//get paperList from DB
		ArrayList<String> paperArrayList = my_frame.db.getPapersAssignedForSC(my_frame.currentUser[0][0]);
		String[] paperArray = new String[paperArrayList.size()];
		for (int i = 0; i < paperArrayList.size(); i++) {
			paperArray[i] = paperArrayList.get(i);
		}
		
		paperList = new JList(paperArray);
		scrollPane.setViewportView(paperList);
		
		JLabel lblNewLabel = new JLabel("Currently Assigned Papers");
		lblNewLabel.setForeground(new Color(138, 43, 226));
		lblNewLabel.setFont(new Font("Urdu Typesetting", Font.PLAIN, 18));
		lblNewLabel.setBounds(41, 0, 267, 31);
		add(lblNewLabel);

		
		JButton downloadButton = new JButton("Download Paper");
		downloadButton.setFont(new Font("Tahoma", Font.PLAIN, 14));
		downloadButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				JFileChooser fc = new JFileChooser();
				fc.showSaveDialog(fc);
				File file = fc.getSelectedFile();
				try {
					CopyDocuments.copy(new File("Papers/" + paperList.getSelectedValue()), file);
				} catch (IOException e1) {
					e1.printStackTrace();
				}
			}
		});
		downloadButton.setBounds(251, 51, 189, 31);
		add(downloadButton);
		
		JButton backButton = new JButton("Back");
		backButton.setFont(new Font("Tahoma", Font.PLAIN, 14));
		backButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//Needs testing
				my_frame.setNewPanel(new SubProgramChairView(my_frame));
				my_frame.repaint();
			
			}
		});
		backButton.setBounds(20, 266, 158, 29);
		add(backButton);
		
		JButton uploadButton = new JButton("Upload Recommendation");
		uploadButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(!paperList.isSelectionEmpty()) {
					File file = my_frame.chooseFileOpen();
					fileLabel.setText(file.getAbsolutePath());
					
				} else {
					JOptionPane.showMessageDialog(null, "Select A Paper PLZ!");
				}
			}
		});
		uploadButton.setFont(new Font("Tahoma", Font.PLAIN, 14));
		uploadButton.setBounds(251, 135, 189, 30);
		add(uploadButton);
		
		fileLabel = new JLabel("");
		fileLabel.setBounds(10, 242, 176, 14);
		add(fileLabel);
		
		JButton submitButton = new JButton("Submit");
		submitButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(fileLabel.getText().equals("")) {
					JOptionPane.showMessageDialog(null, "Upload a paper, please!");
				} else {
					File file = new File(fileLabel.getText());
					try {
						CopyDocuments.copy(file,new File("Recommendations/"+file.getName()));	
						my_frame.db.addRecommendation(my_frame.currentUser[0][0], conferenceName, my_frame.db.getAuthor((String)paperList.getSelectedValue(), conferenceName), file.getName());
						JOptionPane.showMessageDialog(my_frame, "Sucessfully Submitted Recommendation!", "Sucess!", JOptionPane.PLAIN_MESSAGE);
					} catch (IOException e1) {
						e1.printStackTrace();
					}
				}
			}
		});
		submitButton.setFont(new Font("Tahoma", Font.PLAIN, 14));
		submitButton.setBounds(260, 265, 168, 31);
		add(submitButton);
		
		final JLabel pic = new JLabel();
		pic.setSize(500, 150);
		pic.setLocation(0, 300);
		pic.setIcon(new ImageIcon("..\\Conference System\\src\\stripe.gif"));
		
		this.add(pic);
		
		paperfileNameLabel = new JLabel("");
		paperfileNameLabel.setBounds(251, 176, 189, 26);
		add(paperfileNameLabel);
	}
}
